package com.jinkaimen.kqb.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.ShareUrlBean;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.activity.hss.CentralDocumentActivity;
import com.jinkaimen.kqb.ui.activity.hss.ShareQrCodeActivity;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;


/**

 */
public class ShareFragment extends BaseFragment {
    private ImageView mIvCenterRedHint;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_share, null);
    }

    @Override
    protected void initChildView() {
        mIvCenterRedHint = findViewById(R.id.iv_center_red_hint);
//        mIvCenterRedHint.setVisibility(View.VISIBLE);

    }


    @Override
    protected boolean isStaticPage() {
        return true;
    }


    @Override
    protected void setChildViewListener() {
        findViewById(R.id.ll_frag_share_qr_code).setOnClickListener(this);
        findViewById(R.id.ll_frag_share_url).setOnClickListener(this);
        findViewById(R.id.ll_frag_share_center_text).setOnClickListener(this);
    }

    @Override
    protected void initTitleBar() {

    }


    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode) {
            case EventCode.EVENT_REFRESH_SHENHE_STATE:

                break;

        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_frag_share_qr_code:
                ActivityUtils.startActivity(mContext, ShareQrCodeActivity.class);
                break;
            case R.id.ll_frag_share_url:
                ApiManager.getApi().shareUrl("HSS001010").enqueue(new NetCallBack<ShareUrlBean>() {
                    @Override
                    protected void onSuccess(Call<ShareUrlBean> call, Response<ShareUrlBean> response) {
                        ShareUrlBean shareUrlBean = response.body();
                        UMImage image = new UMImage(mContext, "http://7xk9dj.com1.z0.glb.clouddn.com/refreshlayout/images/staggered11.png");//网络图片
                        UMImage thumb = new UMImage(mContext, R.mipmap.ic_launcher);
                        image.setThumb(thumb);
                        UMWeb web = new UMWeb(shareUrlBean.shareUrl);
                        web.setTitle("个人收款神器，免费注册");//标题
                        web.setThumb(thumb);  //缩略图
                        web.setDescription("支持微信、支付宝、QQ，信用卡无卡快捷，银联二维码。大额秒到，还能推广赚钱");//描述
                        new ShareAction(mContext)
                                .withText("hello").withMedia(web)
                                .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
                                .setCallback(shareListener)
                                .open();
                    }
                });
                break;
            case R.id.ll_frag_share_center_text:
                mIvCenterRedHint.setVisibility(View.GONE);
                EventBus.getDefault().post(new EventCenter(EventCode.EVENT_CODE_SHARE_RED_HINT_SHOW));
                ActivityUtils.startActivity(mContext, CentralDocumentActivity.class);
                break;
        }


    }


    @Override
    public void onResume() {
        super.onResume();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(mContext, "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(mContext, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(mContext, "取消了", Toast.LENGTH_LONG).show();

        }
    };
}